/* JavaLog - package tracker 
 * 
 * Copyright (C) 2009 Allyson Lima, Diego Pedro, Victor Freire
 * 
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software 
 * Foundation, either version 3 of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
 * PARTICULAR PURPOSE. See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with this 
 * program. If not, see <http://www.gnu.org/licenses/>.
 */
package br.edu.ufcg.ccc.javalog.XML;

import java.io.File;
import java.util.List;


import br.edu.ufcg.ccc.javalog.exceptions.PersistenceException;
import br.edu.ufcg.ccc.javalog.model.Headquarters;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
import com.thoughtworks.xstream.persistence.FilePersistenceStrategy;
import com.thoughtworks.xstream.persistence.PersistenceStrategy;
import com.thoughtworks.xstream.persistence.XmlArrayList;


/**
 * A PersistenceXMLFranchise Class.
 * 
 * @author Allyson Lima, Diego Pedro, Victor Freire
 * @version 28/11/09
 */
public class PersistenceXMLHeadquarters <S extends Headquarters> extends PersistenceXML<S> {

	private XStream xstream;
	/**
	 * Persist data
	 */
	private PersistenceStrategy strategy;

	/**
	 * Storage data to Xml
	 */
	private List<S> listOfHeadquarters;

	/**
	 * Constructor for PersistenceXMLFranchise Class
	 */
	@SuppressWarnings("unchecked")
	public PersistenceXMLHeadquarters() {
		strategy = new FilePersistenceStrategy(new File(USER_PATH));
		listOfHeadquarters = new XmlArrayList(strategy);
		xstream = new XStream(new DomDriver()); 
	}

	/**
	 * Save the franchise in xml file
	 * 
	 * @param obj
	 *            Franchise to be saved
	 * @throws PersistenceException
	 *             Throw exception for invalid entries
	 */
	@SuppressWarnings("unchecked")
	public void save(S obj) throws PersistenceException {
		if (obj == null)
			throw new PersistenceException("Matriz não informado.");

		if (load(((Headquarters) obj).getName()) != null)			
			throw new PersistenceException("Esta  já existe.");
		
		String convertToXML = xstream.toXML((Headquarters) obj);
		S ss = (S)xstream.fromXML(convertToXML);
		listOfHeadquarters.add(ss);
		
	}

	

	/**
	 * Loads an xml file entry for an Franchise object
	 * 
	 * @param id
	 *            Identifier of the Franchise to be sought
	 * @return The Franchise found
	 * @throws Exception
	 *             Throw exception for invalid entries
	 */
	public S load(Object id) throws PersistenceException {
		if (!(id instanceof String))
			throw new PersistenceException("Invalid ID");

		String StringID = (String) id;

		if (StringID == null || StringID.equals(""))
			throw new PersistenceException("Name null or name empty");

		if(!listOfHeadquarters.isEmpty())
			return listOfHeadquarters.get(0);
		return null;
		
	}


	/**
	 * Returns the total franchises listings
	 * 
	 * @return the total franchises listings
	 */
	public int totalHeadquarters() {
		return listOfHeadquarters.size();
	}
	
	/**
	 * Reset list of the franchises
	 */
	public void clearList() {
		listOfHeadquarters.clear();
	}
	
}
